/*
 * TestJList.java
 *
 * Created on 28 de septiembre de 2008, 11:41 PM
 */

package myListener;

import javax.swing.DefaultListCellRenderer.UIResource;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;

/**
 *
 * @author  manix
 */
public class TestJList extends javax.swing.JFrame {
    
    private DefaultListModel modelo;
    private UIResource posicion;

    public TestJList() {
        this.modelo = new DefaultListModel();
        this.posicion = new UIResource();
        initComponents();
        setLocation(400, 200);
        
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lista = new javax.swing.JList();
        campo = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        botonIzquierda = new javax.swing.JButton();
        botonImagen = new javax.swing.JButton();
        botonDerecha = new javax.swing.JButton();
        botonRemoverPrimero = new javax.swing.JButton();
        botonRemoverUltimo = new javax.swing.JButton();
        botonVaciar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("JList");
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.Y_AXIS));

        jPanel1.setLayout(new java.awt.BorderLayout());

        lista.setModel(modelo);
        lista.setCellRenderer(posicion);
        jScrollPane1.setViewportView(lista);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        campo.setText("insertar desde aqui...");
        campo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoActionPerformed(evt);
            }
        });
        jPanel1.add(campo, java.awt.BorderLayout.PAGE_START);

        getContentPane().add(jPanel1);

        jPanel2.setLayout(new java.awt.GridLayout(2, 3));

        botonIzquierda.setText("Alinear izquierda");
        botonIzquierda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonIzquierdaActionPerformed(evt);
            }
        });
        jPanel2.add(botonIzquierda);

        botonImagen.setText("Agregar imagen");
        botonImagen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonImagenActionPerformed(evt);
            }
        });
        jPanel2.add(botonImagen);

        botonDerecha.setText("Alinear derecha");
        botonDerecha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonDerechaActionPerformed(evt);
            }
        });
        jPanel2.add(botonDerecha);

        botonRemoverPrimero.setText("Remover primero");
        botonRemoverPrimero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonRemoverPrimeroActionPerformed(evt);
            }
        });
        jPanel2.add(botonRemoverPrimero);

        botonRemoverUltimo.setText("Remover ultimo");
        botonRemoverUltimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonRemoverUltimoActionPerformed(evt);
            }
        });
        jPanel2.add(botonRemoverUltimo);

        botonVaciar.setText("Vaciar");
        botonVaciar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonVaciarActionPerformed(evt);
            }
        });
        jPanel2.add(botonVaciar);

        getContentPane().add(jPanel2);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonImagenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonImagenActionPerformed
        ImageIcon imagen = new ImageIcon(getClass().getResource("/imagenes/imagen.png"));
        modelo.addElement(imagen);        
    }//GEN-LAST:event_botonImagenActionPerformed

    private void campoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoActionPerformed
        if (campo.getText().length() > 0) 
            modelo.addElement(campo.getText()); 
        campo.setText("");
}//GEN-LAST:event_campoActionPerformed

    private void botonVaciarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonVaciarActionPerformed
        modelo.clear();
}//GEN-LAST:event_botonVaciarActionPerformed

    private void botonRemoverUltimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonRemoverUltimoActionPerformed
        try {
            modelo.removeElementAt(modelo.size() - 1);

        } catch (Exception exception) {
            System.out.println("No hay elementos");
        }

    }//GEN-LAST:event_botonRemoverUltimoActionPerformed

    private void botonIzquierdaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonIzquierdaActionPerformed
        posicion.setHorizontalAlignment(SwingConstants.LEFT);
        repaint();
}//GEN-LAST:event_botonIzquierdaActionPerformed

    private void botonRemoverPrimeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonRemoverPrimeroActionPerformed
        try {
            modelo.removeElementAt(0);
        } catch (Exception exception) {
            System.out.println("No hay elementos");
        }
    }//GEN-LAST:event_botonRemoverPrimeroActionPerformed

    private void botonDerechaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonDerechaActionPerformed
        posicion.setHorizontalAlignment(SwingConstants.RIGHT);
        repaint();
    }//GEN-LAST:event_botonDerechaActionPerformed

    public static void main(String args[]) {
        TestJList ventana = new TestJList();
        ventana.setVisible(true);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonDerecha;
    private javax.swing.JButton botonImagen;
    private javax.swing.JButton botonIzquierda;
    private javax.swing.JButton botonRemoverPrimero;
    private javax.swing.JButton botonRemoverUltimo;
    private javax.swing.JButton botonVaciar;
    private javax.swing.JTextField campo;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList lista;
    // End of variables declaration//GEN-END:variables
    
}
